mui.init({
	pullRefresh: {
		container: '#offCanvasContentScroll',

		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}
	}
});

window.addEventListener("Recommended", function(e) {
	alert("e")
})

var bookshelf = new Vue({
	el: '.main',
	data: {
		cls: [],
		booksl: [],
		pingfen: [],
		trues: [],
		texts: []
	},
	updated: function() {
		$(".mainclass").on("tap", function(e) {
			if(!$(e.target).is('button')) {
				common.open("../bookdetails/details.html", "", $(this).attr("bookUid"))
				localStorage.setItem("aa", $(this).attr("bookUid"))
			}
		})
	}
});

$(document).on("tap", ".add", function(e) {
	if($(this).hasClass("added")) {
		return
	}
	var self = $(this)
	common.ajax("student/myShelf/addBook", {
		uid: common.uid,
		token: common.token,
		bookUid: $(this).parents(".mainclass").attr("bookuid")
	}, function(data) {
		if(data.code == 200) {
			self.text("已添加")
			self.addClass("added")
			var load = plus.webview.getWebviewById("bookshelf.html");
			mui.fire(load, "index");
		} else {
			mui.toast(data.message)
		}
	})
	e.stopPropagation()
})

var mask = mui.createMask(reduction)
$(".select").on("tap", ".mui-btn", function() {
	mask.show()
	var index = $(this).index()
	reduction()
	if($(".genre").eq(index).height() != 0) {
		mask.close()
		return
	}
	var height = $(".genre").eq(index).find("div").eq(0).height() * $(".genre").eq(index).find("div").length
	$(".genre").eq(index).css("height", height)
	$(".down").eq(index).css("transform", "rotate(180deg)")
	$(this).find("div").css({
		"color": "#21B0FD",
		"border-top-color": "#21B0FD"
	})
})

function reduction() {
	$(".genre").css("height", 0)
	$(".down").css("transform", "rotate(0deg)")
	$(".select").find("div").css({
		"color": "#333",
		"border-top-color": "#333"
	})
}
$(".typemain .popular").on("tap", "div", function() {
	$(this).parents(".typemain").prev().find(".selectiontype").eq(1).attr("uid", $(this).attr('uid'))
})
common.ajax("book/category", {
	uid: common.uid,
	token: common.token
}, function(data) {
	if(data.code == 200) {
		bookshelf.cls = data.data
	} else {
		mui.toast(data.message)
	}
})

var temp = 1

$(".genre").on("tap", "div", function() {
	var parent = $(this).parent().index()
	reduction()
	mask.close()
	$(".selectiontype").eq(parent).text($(this).text())
	bookshelf.trues = []
	bookshelf.booksl = []
	bookshelf.pingfen = []
	var grad = $(".selectiontype").eq(0).text() == "适合年级" ? "" : $(".selectiontype").eq(0).text()
	var types = $(".selectiontype").eq(1).attr("uid") == null ? "" : $(".selectiontype").eq(1).attr("uid")

	common.ajax('student/bookCity/queryRecommendBook', {
		uid: common.uid,
		token: common.token,
		grade: grad,
		categoryUid: types,
		page: 1,
		pageSize: "8"

	}, function(data) {
		if(data.code == 200) {
			//激活下拉刷新
			temp = 1
			mui('#offCanvasContentScroll').pullRefresh().refresh(true);
			datafor(data)
			if(!data.data.hasNextPage) {
				$(".mui-pull-bottom-pocket").css("visibility", "inherit")
				$(".mui-pull").text("已经没有更多了")
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true)
			}
		} else {
			mui.toast(data.message)
		}
	}, 'get')
})

/**
 * 上拉加载具体业务实现
 */
function pullupRefresh() {
	var self = this
	var grad = $(".selectiontype").eq(0).text() == "适合年级" ? "" : $(".selectiontype").eq(0).text()
	var types = $(".selectiontype").eq(1).attr("uid") == null ? "" : $(".selectiontype").eq(1).attr("uid")
	common.ajax('student/bookCity/queryRecommendBook', {
		uid: common.uid,
		token: common.token,
		grade: grad,
		categoryUid: types,
		page: temp++,
		pageSize: "8"
	}, function(data) {
		if(data.code == 200) {
			mui('#offCanvasContentScroll').pullRefresh().refresh(true);
			if(!data.data.hasNextPage) {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh(true)
			} else {
				mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh()
			}
			datafor(data)
		} else {
			mui.toast(data.message)
		}
	}, 'get')
}

$('.bookcity', window.parent.document).on("tap", function() {

	temp = 1
	bookshelf.trues = []
	bookshelf.booksl = []
	bookshelf.pingfen = []
	shauxin()
})

setTimeout(function() {
	shauxin()
}, 30)

function shauxin() {
	if(mui.os.plus) {
		mui.plusReady(function() {
			setTimeout(function() {
				mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
			}, 300);

		});
	} else {
		mui.ready(function() {
			mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
		});
	}

}

function datafor(data) {
	//	console.log(data)
	for(var i = 0; i < data.data.list.length; i++) {
		if(String(data.data.list[i].mark).indexOf(".") != -1) {
			bookshelf.trues.push("scoring_two")
		} else {
			bookshelf.trues.push("")
		}
		bookshelf.booksl.push(data.data.list[i])
		bookshelf.pingfen.push(Math.floor(data.data.list[i].mark))
	}
}